package com.darren.euc.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.darren.euc.dao.HanWangDoorDao;
import com.darren.euc.dao.MemberDao;
import com.darren.euc.entity.HanWangDoor;
import com.darren.euc.entity.Member;
import com.darren.euc.entity.PageBean;
import com.darren.euc.service.HanWangDoorService;
import com.darren.euc.util.HanWangUtil;
import com.darren.euc.util.StringUtil;

import net.sf.json.JSONObject;


@Service
public class HanWangDoorServiceImpl implements HanWangDoorService {
	
	@Autowired
    private HanWangDoorDao hanWangDoorDao;
	
	@Autowired
	private MemberDao memberDao;
	
    @Override
    public HanWangDoor save(HanWangDoor hanWangDoor) {
        return hanWangDoorDao.save(hanWangDoor);
    }

    @Override
    public HanWangDoor update(HanWangDoor hanWangDoor) {
        return hanWangDoorDao.update(hanWangDoor);
    }
    
    @Override
    public HanWangDoor findById(Integer hanWangDoorId){
    	return hanWangDoorDao.findById(hanWangDoorId);
    }
    
    @Override
    public void delete(HanWangDoor hanWangDoor) {
    	hanWangDoorDao.delete(hanWangDoor);
    }
    
	@Override
	public List<HanWangDoor> getAll() {
		return hanWangDoorDao.getAll();
	}
	
	@Override
	public int updateHanWangConnectionStatus(Integer id,Integer connectionStatus){
		return hanWangDoorDao.updateHanWangConnectionStatus(id, connectionStatus);
	}

	@Override
	public List<HanWangDoor> getMainHanWangDoorBySpaceIdNotEquelsId(Integer spaceId,Integer hanwangId) {
		return hanWangDoorDao.getMainHanWangDoorBySpaceIdNotEquelsId(spaceId,hanwangId);
	}

	@Override
	public List<HanWangDoor> getMainHanWangDoorBySpaceId(Integer spaceId){
		return hanWangDoorDao.getMainHanWangDoorBySpaceId(spaceId);
	}
	
	@Override
	public List<HanWangDoor> getHanWangDoorByMemberId(Integer memberId){
		return hanWangDoorDao.getHanWangDoorByMemberId(memberId);
	}
	
	@Override
	public Map<String,Object> hanWangDoorDevice(HanWangDoor hanWangDoor){
		Map<String,Object> deviceInfo = new HashMap<String,Object>();
		try{
			deviceInfo.put("ip", StringUtil.convertNullToEmptyString(hanWangDoor.getIp()));
			deviceInfo.put("port", hanWangDoor.getPort());
			deviceInfo.put("password", StringUtil.convertNullToEmptyString(hanWangDoor.getPassword()));
			deviceInfo.put("deviceName", StringUtil.convertNullToEmptyString(hanWangDoor.getDeviceName()));
			deviceInfo.put("connectStatus", hanWangDoor.getConnectStatus());
		}catch(Exception ex){
			System.out.println(ex.getMessage());
		}		
		return deviceInfo;
	}
	
	@Override
	public String bindingCard(Integer memberId){
		String message = "";
		Member member = memberDao.findById(memberId);
		if("".equals(StringUtil.convertNullToEmptyString(member.getWristBandsNumber()))){
			message = String.format("会员【%s】的卡号为空,请核对后再进行操作!",member.getName());
			return message;
		}
		Map<String,Object> memberInfo = new HashMap<String,Object>();
		memberInfo.put("name", StringUtil.convertNullToEmptyString(member.getName()));
		List<HanWangDoor> hanWangDoors =  hanWangDoorDao.getMainHanWangDoorBySpaceId(member.getSpaceId());
		if(hanWangDoors != null && hanWangDoors.size() > 0){
			Map<String,Object> mainDevice = hanWangDoorDevice(hanWangDoors.get(0));
			List<HanWangDoor> others =  hanWangDoorDao.getHanWangDoorByMemberId(member.getId());
			List<Map<String,Object>> otherDevices = new ArrayList<Map<String,Object>>();
			for(HanWangDoor other:others){
				Map<String,Object> ortherDevice = hanWangDoorDevice(other);
				otherDevices.add(ortherDevice);
			}
			message = HanWangUtil.bindHandCard(mainDevice, otherDevices, Integer.parseInt(member.getWristBandsNumber()), memberInfo);
			member.setIcCard((String)memberInfo.get("card_num"));
			memberDao.update(member);
		}else{
			message = "未找到主设备!";
		}		
		return message;
	}
	
	@Override
	public String unbindingCard(Integer memberId){
		String message = "";
		Member member = memberDao.findById(memberId);
		if("".equals(StringUtil.convertNullToEmptyString(member.getWristBandsNumber()))){
			message = String.format("会员【%s】的卡号为空,请核对后再进行操作!",member.getName());
			return message;
		}
		
		List<Map<String,Object>> devices = new ArrayList<Map<String,Object>>();
		List<HanWangDoor> hanWangDoors =  hanWangDoorDao.getMainHanWangDoorBySpaceId(member.getSpaceId());
		if(hanWangDoors == null || hanWangDoors.size() == 0){
			message = "未找到主设备!";
			return message;
		}
		Map<String,Object> mainDevice = hanWangDoorDevice(hanWangDoors.get(0));
		devices.add(mainDevice);
		List<HanWangDoor> others =  hanWangDoorDao.getHanWangDoorByMemberId(member.getId());			
		for(HanWangDoor other:others){
			Map<String,Object> ortherDevice = hanWangDoorDevice(other);
			devices.add(ortherDevice);
		}
		message = HanWangUtil.unbindHandCard(devices, Integer.parseInt(member.getWristBandsNumber()));
		return message;
	}
	
	@SuppressWarnings("rawtypes")
	public PageBean queryForPageUseHQL(JSONObject requestJson,Integer pageSize, Integer currentPage){		
		String HQL = null;
		String likes = null;
		String equals = null;
		List<Object> parameters = new ArrayList<Object>();
		JSONObject likeJson = requestJson.getJSONObject("like");
		Iterator likeIterator = likeJson.keys();
		while(likeIterator.hasNext()){
			String key = (String) likeIterator.next();
			key = StringUtil.convertNullToEmptyString(key);
			String value = likeJson.getString(key);
			value = StringUtil.convertNullToEmptyString(value);
			if(!"".equals(value)){
				likes = (likes == null?"": likes + " OR ") +  key + " like ? ";
				parameters.add("%"+value+"%");
			}
		}
		HQL = likes == null?null:(" (" + likes + ") ");		
		JSONObject equalsJson = requestJson.getJSONObject("equals");
        Iterator equalsIterator = equalsJson.keys();        
		while(equalsIterator.hasNext()){
			String key = (String) equalsIterator.next();
			key = StringUtil.convertNullToEmptyString(key);
			String value = equalsJson.getString(key);
			value = StringUtil.convertNullToEmptyString(value);
			if(!"".equals(value)){
				equals = (equals == null?"": ""+equals + " and ") +  key + " = ? ";
				if("spaceId".equals(key) || "connectStatus".equals(key)){
					parameters.add(Integer.parseInt(value));
				}else{
					parameters.add(value);
				}
			}
		}		
		if(HQL != null){
			if(equals != null){
				HQL = HQL + " and " + equals;
			}
		}else{
			HQL = equals;
		}
		HQL = " from HanWangDoor where enable = true " + (HQL == null?"":" and  " + HQL) + " order by lastModifyTime desc ";
		return hanWangDoorDao.queryForPageByHQL(HQL, pageSize, currentPage,parameters.toArray());
	}


	@Override
	public String bindingEmployeeCard(Integer spaceId){
		String message = "";
		List<HanWangDoor> hanWangDoors =  hanWangDoorDao.getMainHanWangDoorBySpaceId(spaceId);
		if(hanWangDoors != null && hanWangDoors.size() > 0){
			Map<String,Object> mainDevice = hanWangDoorDevice(hanWangDoors.get(0));
			List<HanWangDoor> others =  hanWangDoorDao.getOtherHanWangDoorBySpaceId(spaceId);
			List<Map<String,Object>> otherDevices = new ArrayList<Map<String,Object>>();
			for(HanWangDoor other:others){
				Map<String,Object> ortherDevice = hanWangDoorDevice(other);
				otherDevices.add(ortherDevice);
			}
			message = HanWangUtil.bindEmployeeHandCard(mainDevice, otherDevices);
		}else{
			message = "未找到主设备!";
		}		
		return message;
	}
	
	@Override
	public String updateHanWangManager(Integer spaceId){
		String message = "";
		List<HanWangDoor> hanWangDoors =  hanWangDoorDao.getMainHanWangDoorBySpaceId(spaceId);
		if(hanWangDoors != null && hanWangDoors.size() > 0){
			Map<String,Object> mainDevice = hanWangDoorDevice(hanWangDoors.get(0));
			List<HanWangDoor> others =  hanWangDoorDao.getOtherHanWangDoorBySpaceId(spaceId);
			List<Map<String,Object>> otherDevices = new ArrayList<Map<String,Object>>();
			for(HanWangDoor other:others){
				Map<String,Object> ortherDevice = hanWangDoorDevice(other);
				otherDevices.add(ortherDevice);
			}
			message = HanWangUtil.updateHanWangManager(mainDevice, otherDevices);
		}else{
			message = "未找到主设备!";
		}		
		return message;
	}

}